package com.yaozheng.vocationaltraining.db;

import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class JSONDBCacheDao {
	/**
	 * 表名字
	 */
	public static final String TABLE_NAME = "json_db_cache";

	/**
	 * 模块内容
	 */
	public static final String MODULES_NAME = "modules_name";
	/**
	 * 内容
	 */
	public static final String CONTENT = "content";
	/**
	 * 附加内容
	 */
	public static final String ADDITIONAL_CONTENT = "additional_content";
	/**
	 * 状态
	 */
	public static final String STATUS = "status";
	/**
	 * 创建表sql
	 */
	public static final String CREATE_TABLE_SQL = "create table if not exists "
			+ TABLE_NAME + " (" + MODULES_NAME + " TEXT  PRIMARY KEY, "
			+ CONTENT + " TEXT," + ADDITIONAL_CONTENT + " TEXT," + STATUS
			+ " integer );";
	protected DbOpenHelper dbHelper;

	public JSONDBCacheDao(BaseActivity baseActivity) {
		dbHelper = DbOpenHelper.getInstance(baseActivity);
	}

	protected JSONDBCacheDao() {
		super();
	}

	/**
	 * 得到json缓存数据
	 * 
	 * @param modulesName
	 *            模块
	 * @return
	 * @throws Exception
	 */
	public synchronized JSONObject getJSONDBCache(String modulesName)
			throws Exception {
		SQLiteDatabase db = null;
		Cursor cursor = null;

		try {
			db = dbHelper.getReadableDatabase();

			if (db.isOpen()) {
				cursor = db.rawQuery("select * from " + TABLE_NAME + " where "
						+ MODULES_NAME + "='" + modulesName + "'", null);
				while (cursor.moveToNext()) {
					return bindJsonDBCache(cursor);
				}

			}
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
			if (db != null && db.isOpen()) {
				db.close();
				db = null;

			}
		}
		return null;
	}

	/**
	 * 绑定jsonDb
	 * 
	 * @param cursor
	 * @return
	 */
	private JSONObject bindJsonDBCache(Cursor cursor) {
		JSONObject jsonObject = new JSONObject();

		jsonObject.put(MODULES_NAME,
				cursor.getString(cursor.getColumnIndex(MODULES_NAME)));
		jsonObject.put(CONTENT,
				cursor.getString(cursor.getColumnIndex(CONTENT)));
		jsonObject.put(ADDITIONAL_CONTENT,
				cursor.getString(cursor.getColumnIndex(ADDITIONAL_CONTENT)));
		jsonObject.put(STATUS, cursor.getString(cursor.getColumnIndex(STATUS)));
		return jsonObject;
	}

	/**
	 * 删除数据库中的json
	 * 
	 * @param username
	 */
	public synchronized void deleteJSONDBCache(String modulesName) {
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			if (db.isOpen()) {
				db.delete(TABLE_NAME, MODULES_NAME + " = ?",
						new String[] { modulesName });
			}
		} finally {
			if (db != null && db.isOpen()) {
				db.close();
				db = null;

			}
		}
	}

	/**
	 * 把json缓存到数据库中
	 * 
	 * @param user
	 */
	public void saveJSONDBCache(JSONObject data) throws Exception {
		SQLiteDatabase db = null;

		try {

			db = dbHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(MODULES_NAME,
					TypeUtils.getJsonString(data, MODULES_NAME, ""));
			values.put(CONTENT, TypeUtils.getJsonString(data, CONTENT, ""));
			values.put(ADDITIONAL_CONTENT,
					TypeUtils.getJsonString(data, ADDITIONAL_CONTENT, ""));
			values.put(STATUS, TypeUtils.getJsonString(data, STATUS, ""));
			db.replace(TABLE_NAME, null, values);

		} finally {
			if (db != null && db.isOpen()) {
				db.close();
				db = null;

			}
		}

	}
	

}
